package my_a10bbc;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Calendar;

import sdstcom.lbq.one.R;

public class HistoryActivity extends AppCompatActivity implements AdapterView.OnItemClickListener,RadioGroup.OnCheckedChangeListener{
    private Toolbar tb;
    private Calendar calendar;
    private ArrayList<String> list;
    private ArrayAdapter<String> adapter;
    private ArrayList<String> date;
    private ListView lv;
    private String msg="hp";
    private RadioGroup rg;
    //kldsajdklasjkljklasfklasd
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_ACTION_BAR);
        setContentView(R.layout.activity_history);
        init();
        tb=(Toolbar)findViewById(R.id.tb_history);
        tb.setNavigationIcon(R.mipmap.ic_keyboard_backspace_black_24dp);
        tb.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                exit();
            }
        });



    }

    private void init() {

        rg=(RadioGroup)findViewById(R.id.rg_history);
        rg.setOnCheckedChangeListener(this);
        lv=(ListView)findViewById(R.id.lv_history);
        list=new ArrayList<>();
        date=new ArrayList<>();
        loaddata();
        adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(this);
    }

    private void loaddata() {
        final String[] monthArr=getResources().getStringArray(R.array.month);
        calendar=Calendar.getInstance();
        int year=calendar.get(Calendar.YEAR);
        int month=calendar.get(Calendar.MONTH);

            list.add("本月");
            date.add(year+"-0"+(month+1)+"-"+"01");
        for (int i = month-1; i >=0 ; i--) {
            list.add(monthArr[i]+"."+year);
            if (i>8) {
                date.add(year + "-" + i + "-" + "01");
            }else{
                date.add(year + "-0" + i + "-" + "01");
            }
        }
        for (int i=year-1;i>year-5;i--){
            for (int j = 11; j >=0; j--) {
                list.add(monthArr[j]+"."+i);

                if (j>=9){
                    date.add(i+"-"+(j+1)+"-"+"01");
                }else{
                    date.add(i+"-0"+(j+1)+"-"+"01");
                }

            }
        }
    }

//

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void exit() {
//        Intent intent=new Intent(this,MainActivity.class);
//        startActivity(intent);
        setResult(200);
        finish();
        overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right);

    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        final String[] arrtype=getResources().getStringArray(R.array.type_news);
        rg.check(checkedId);

        msg=arrtype[checkedId];




        Toast.makeText(HistoryActivity.this, ""+(checkedId-1), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent=new Intent(this,InfolistActivity.class);
        intent.putExtra("date",date.get(position));
        intent.putExtra("type",msg);
        intent.putExtra("title",list.get(position).toString());
        startActivity(intent);

    }
}
